<template>
  <div class="app-container">
    <div>
      <div slot="header" class="clearfix job-tab-header">
        <span class="span-title">子系统管理</span>
      </div>
      <div>
        <div class=" select-container" style="margin-top:-15px">
          <el-row class="">
            <el-col class="">
              <el-form ref="form"  label-position='right' size="small" :inline="true" label-width="110px">
                <el-col :span="5" >
                  <el-form-item label="" prop="en_subsys_name" size='small'  style="margin:12px 0px;">
                      <el-input
                        size="small"
                        style="width: 200px;"
                        class="filter-item"
                        v-model="nameEn"
                        placeholder="输入子系统英文名称"
                        clearable
                        @change="entitle_change"
                        prefix-icon="el-icon-search"
                      ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="5" >
                  <el-form-item label="" prop="subsys_name" size='small'  style="margin:12px 0px;">
                      <el-input
                        size="small"
                        style="width: 200px;"
                        class="filter-item"
                        v-model="name"
                        placeholder="输入子系统名称"
                        clearable
                        @change="title_change"
                        prefix-icon="el-icon-search"
                        ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="14" style="text-align: left;">
                  <el-button size="small" style="height:30px;padding-top:0px;line-height:30px;margin:12px 0px;" type="success" icon="el-icon-edit" @click="getList(selectOuinfoId)">查 询</el-button>
                  <el-button style="height:30px;padding-top:0px;line-height:30px;margin:12px 0px;" size="small" type="primary" @click="createClick()">创建子系统</el-button>
                </el-col>
              </el-form>
            </el-col>
          </el-row>
        </div>
      </div>
    </div>
      <div>
        <el-table
          :key='tableKey'
          :data="list.records"
          v-loading="listLoading"
          element-loading-text="给我一点时间"
          size="small"
          height="310px"
          stripe
          style="width: 100%;border:#eeeeee"
          highlight-current-row>
          <el-table-column
            align="center"
            type="index"
            width="80"
            label="序号"
          >
            <template slot-scope="scope">
            <span>{{scope.$index + (serialNumber - 1) * 10 + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="子系统英文名"
            width="180"
            prop="nameEn"
          >
          </el-table-column>

          <el-table-column
            align="center"
            label="子系统名称"
            prop="name"
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="子系统访问URL"
            width="300"
            prop="url"
          >
          </el-table-column>
          <!-- <el-table-column
            align="center"
            label="最后修改人"
            prop="lmUser"
          >
          </el-table-column> -->
          <el-table-column
            align="center"
            label="创建时间"
            prop="fcTime"
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="排序"
            prop="orderCode"
          >
          </el-table-column>
          <el-table-column align="center" label="操 作" width="220px" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="danger" size="mini" @click="handleDelete(scope.row,'delete')">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container">
          <el-pagination
            layout="total, prev, pager, next, jumper"
            :page-size = "this.postList.rowBounds.limit"
            :total="this.list.count"
            @current-change="current_change"
          >
          </el-pagination>
        </div>
      </div>
    <!--创建子系统弹出层-->
    <el-dialog
      v-if="dialogVisibleAddSubSys"
      :close-on-click-modal="false"
      title="创建子系统"
      :visible.sync="dialogVisibleAddSubSys"
      width="70%">
      <SubSystemAdd @changeCloseDialog="changeCloseDialog"></SubSystemAdd>
    </el-dialog>
    <!--编辑子系统弹出层-->
    <el-dialog
      v-if="dialogVisibleEditSubSys"
      :close-on-click-modal="false"
      title="编辑子系统"
      :visible.sync="dialogVisibleEditSubSys"
      width="70%">
      <SubSystemUpdate :listForm="listForm" @changeCloseDialog="changeCloseDialog"></SubSystemUpdate>
    </el-dialog>
  </div>
</template>

<script>
import '@/styles/newstyle.scss'
import SubSystemAdd from './components/subSystemAdd'
import SubSystemUpdate from './components/subSystemUpdate'
const nameCondition = {
  'property': 'name',
  'value': '',
  'type': 'LIKE'
}
const ennameCondition = {
  'property': 'nameEn',
  'value': '',
  'type': 'LIKE'
}
export default {
  components: {
    SubSystemAdd,
    SubSystemUpdate
  },
  data() {
    return {
      dialogVisibleAddSubSys: false,
      dialogVisibleEditSubSys: false,
      listForm: {},
      name: '',
      nameEn: '',
      dappendVisible: false,
      updateVisible: false,
      tableKey: 0,
      list: {},
      postList: {
        condition: [],
        sort: [{
          direction: 'ASC',
          property: 'orderCode'
        }],
        rowBounds: {
          offset: 0,
          limit: 10
        }
      },
      listLoading: false,
      fliter: [
        Object.assign({}, nameCondition),
        Object.assign({}, ennameCondition)
      ],
      deletelist: {},
      serialNumber: 1,
    }
  },
  // 画面刚进入method
  created() {
    this.fetchData()
  },
  methods: {
    changeCloseDialog() {
      this.dialogVisibleAddSubSys = false
      this.dialogVisibleEditSubSys = false
      this.fetchData()
    },
    fetchData(flag) {
      this.listLoading = true
      if (flag ==='delete') {
        if (this.postList.rowBounds.offset == (this.list.count-1)
         && this.postList.rowBounds.offset >= this.postList.rowBounds.limit) {
          this.postList.rowBounds.offset -=this.postList.rowBounds.limit
          this.serialNumber -=1
        }
      }
      this.$store.dispatch('SnbsysSelect', this.postList).then(res => {
        this.list = res.data
        this.list.records = res.data.records.map(item => {
          // item.fcTime = this.formatDate(item.fcTime)
          item.fcTime = item.fcTime.slice(0, 8)
          item.fcTime = this.formatDate(item.fcTime)
          return item
        })
        this.listLoading = false
      }).catch((err) => {
        console.log(err)
      })
    },
    getList(val) {
      this.serialNumber =1
      this.postList.rowBounds.offset = 0
      this.list =''
      this.fetchData()
    },
    formatDate(datestr) {
      // var datetime = new Date()
      // datetime.setTime(datestr)
      var year = datestr.slice(0, 4)
      var month = datestr.slice(4, 6)
      var date = datestr.slice(6, 8)

      return year + '-' + month + '-' + date
    },

    deleteData(flag) {
      this.$store.dispatch('SnbsysDelete', this.deletelist).then(res => {
        this.fetchData(flag)
        if (res.success) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } else {
          this.$message({
            type: 'success',
            message: res.data.message
          })
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    handleDelete(row,flag) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deletelist.id = row.id
        this.deleteData(flag)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    createClick() {
      this.dialogVisibleAddSubSys = true
    },
    handleEdit(row) {
      this.listForm = row
      this.dialogVisibleEditSubSys = true
    },
    current_change(currentPage) { // 通过分页获取数据
      this.serialNumber = currentPage
      const page = currentPage - 1
      this.postList.rowBounds.offset = page * 10
      this.fetchData()
    },
    title_change(type) { // 分类数据
      this.serialNumber =1
      if (type === null) {
        const nullfliter = this.fliter.concat()
        const condition = []
        for (let i = 0; i < nullfliter.length; i++) {
          if (nullfliter[i].property === 'name') {
            nullfliter.splice(i, 1)
            this.fliter[i].value = ''
          }
        }
        for (let i = 0; i < nullfliter.length; i++) {
          if (nullfliter[i].value === '') {
            continue
          }
          condition.push(nullfliter[i])
        }
        this.postList.condition = condition
        this.postList.rowBounds = {
          'offset': 0,
          'limit': 10
        }
        this.fetchData()
      } else {
        const conditionTimefliter = this.fliter.concat()
        const condition = []
        for (let i = 0; i < conditionTimefliter.length; i++) {
          if (conditionTimefliter[i].property === 'name') {
            conditionTimefliter[i].value = type
            this.fliter[i].value = type
          }
        }
        for (let i = 0; i < conditionTimefliter.length; i++) {
          if (conditionTimefliter[i].value === '') {
            continue
          }
          condition.push(conditionTimefliter[i])
        }
        this.postList.condition = condition
        this.postList.rowBounds = {
          'offset': 0,
          'limit': 10
        }
        this.fetchData()
      }
    },
    entitle_change(type) { // 分类数据
      this.serialNumber =1
      if (type === null) {
        const nullfliter = this.fliter.concat()
        const condition = []
        for (let i = 0; i < nullfliter.length; i++) {
          if (nullfliter[i].property === 'nameEn') {
            nullfliter.splice(i, 1)
            this.fliter[i].value = ''
          }
        }
        for (let i = 0; i < nullfliter.length; i++) {
          if (nullfliter[i].value === '') {
            continue
          }
          condition.push(nullfliter[i])
        }
        this.postList.condition = condition
        this.postList.rowBounds = {
          'offset': 0,
          'limit': 10
        }
        this.fetchData()
      } else {
        const conditionTimefliter = this.fliter.concat()
        const condition = []
        for (let i = 0; i < conditionTimefliter.length; i++) {
          if (conditionTimefliter[i].property === 'nameEn') {
            conditionTimefliter[i].value = type
            this.fliter[i].value = type
          }
        }
        for (let i = 0; i < conditionTimefliter.length; i++) {
          if (conditionTimefliter[i].value === '') {
            continue
          }
          condition.push(conditionTimefliter[i])
        }
        this.postList.condition = condition
        this.postList.rowBounds = {
          'offset': 0,
          'limit': 10
        }
        this.fetchData()
      }
    }
  }
}
</script>

<style scoped>
  .container{
    color: #3b96f3;
    background: #fff;
    padding: 20px;
    height: 100%;
    overflow-y: auto;
  }
  .select-container{
    padding: 10px 10px;
    margin-bottom: 10px;
    border-radius: 4px;
    border: 1px solid #eeeeee;
  }
  .list-container{
    margin-top:30px;
    padding:0 10px 10px 10px;
    border:1px solid#eeeeee;
    border-radius: 4px;
  }
  .data-contant{
    background-color: rgba(0,0,0,.035);
    padding: 30px 10px 20px;
  }
  .data-detail{
    font-size: 14px;
    padding:20px 0;
    text-align: center;
  }
  .data-title{
    font-size: 14px;
    padding:10px 0;
    text-align: center;
  }
  .data-title div{
    font-size: 14px;
    padding:5px 0;
  }
  .filter{
    padding: 10px 5px;
    border-bottom: 2px solid#eeeeee;
  }
  .list-container{
  }
  .bold{
    font-weight: bold;
  }
</style>
